<?php
class Tos_model_menu extends Model {
    private $table_menu_item = 'menu_item';
    
    public function get_menu_items($menu_name) {
        $menu_name = trim($menu_name);
        if (empty($menu_name)) tos_die();
        
        $rows = $this->db->select_rows(
            $this->table_menu_item,
            array(),
            array('menu_name' => $menu_name),
            array(),
            array('rank'=>'DESC')
        );
        $items = array();
        
        foreach ($rows as $row) {
            $parent_id = (int) $row['parent_id'];
            $id = (int) $row['id'];
            $items[$parent_id][$id] = $row;
        }
        
        foreach ($items as &$sub_items) {
            foreach ($sub_items as &$item) {
                $item_id = (int) $item['id'];
                if (isset($items[$item_id])) {
                    $item['children'] = $items[$item_id];
                }
            }
        }
        
        
        return $items[0];
    }
}
?>